// pages/index1/index1.js

var app = getApp()
Page({
  /**
   * 页面的初始数据 
   */
  data: {
    loading: false,
    plain: false,
    disabled: false,//按钮一旦点击提交 则不能操作
    wxid: '',//微信id
    scindex:0,//当前选中的学校编号
    schs: [],//所有校区
    sname:'',//儿童姓名
    sfzh:'',//儿童身份证号
    birth: '',//生日
    sex: '',//性别
    jzxm: '',//家长姓名
    jzsj:'',//家长手机

    region: [],//所有校区
    arindex: 0,//当前选中的地区编号

    sfkqbm:'',//是否开启登记
    rxstart: '', //儿童入学生日起
    rxend: '', //儿童入学生日止
    bmjzrq: '',//登记截止日期

    imgs: [],//待上传的图片(本地)
    upurls: [],//已经上传了的图片
    formid: '',
  }, 


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var sfkqbm = wx.getStorageSync('sfkqbm');
   if (sfkqbm == "1") {
      wx.showModal({
        title: '温馨提示',
        content: '新生登记结束，等待摇号！',
        showCancel: !1,
        success(res) {
          if (res.confirm) {
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })
      return;
    }

    wx.showLoading({
      title: '加载中',
    })

    //查询所有学校
    this.getAllSchool();
    //查询播州区的镇/街 以及 社区/村
    this.getBzqArea();
    var wxid = wx.getStorageSync('openid');
    this.setData({
      wxid: wxid
    })
  }, 

  getAllSchool: function (e){
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/getAllSchool.html',//请求地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var schs = [{CSID:'',CSNAME:'请选择学校'}];
        for (var i = 0; i < res.data.length; i++) {
          var sch = res.data[i];
          schs.push(sch);
        }
        that.setData({
          schs: schs
        });
      }
    })
  },


  getBzqArea: function (e) {
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/getBzqArea.html',//请求地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          region: res.data
        });
      
        var html1 ="1.登记幼儿户籍必须在以下社区：白龙社区、象山社区、白锦社区、南园社区、南白社区、遵南社区、娄山社区、桂花社区、龙泉社区\n\n";
        var rxstart = wx.getStorageSync('rxstart');
        var rxend = wx.getStorageSync('rxend');
        if (rxstart != "" && rxend != "") {
          html1 += "2.登记儿童出生年月为" + rxstart + "至" + rxend+"之间\n\n";
        }else{
          html1 += "2.登记儿童年龄为2-3岁\n\n";
        }
        var bmjzrq = wx.getStorageSync('bmjzrq');
      
        if (bmjzrq != "") {
          html1 += "3.登记截止时间为" + bmjzrq;
        }
        that.setData({
          rxstart: rxstart,
          rxend: rxend,
          bmjzrq: bmjzrq,
        })
        wx.hideLoading();

        wx.showModal({
          title: '温馨提示',
          content: html1,
          showCancel: !1,
        })
      }
    })
  },


  //选中学校
  bindPickerChange: function (e) {
    this.setData({
      scindex: e.detail.value
    })
  },
//获取儿童姓名
  snameinput: function (e) {
    this.setData({
      sname: e.detail.value
    })
  },

  //获取儿童身份证号码
  sfzinput: function (e) {
    this.setData({
      sfzh: e.detail.value
    })
  },

  //获取儿童生日
  birthinput: function (e) {
    this.setData({
      birth: e.detail.value
    })
  },

  //性别
  sexChange(e) {
    this.setData({
      sex: e.detail.value
    })
  },

  //家长姓名
  jzxminput: function (e) {
    this.setData({
      jzxm: e.detail.value
    })
  },
  //家长手机号
  jzsjhinput: function (e) {
    this.setData({
      jzsj: e.detail.value
    })
  },



  //选中地区
  hjszdPickerChange: function (e) {
    this.setData({
      arindex: e.detail.value
    })
  },

  
  //上传图片
  uploadimage: function () {
    var that = this;
    wx.chooseImage({
      count: 4, //张数， 默认9
      sizeType: ['compressed'], //建议压缩图
      sourceType: ['album', 'camera'], // 来源是相册、相机
      success: function (res) {
        var imgs = that.data.imgs;
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          imgs.push(res.tempFilePaths[i]);
        }
        that.setData({
          imgs: imgs
        })
      }
    });
  },


  //删除图片
  delImage: function (e) {
    var that = this;
    var imgs = that.data.imgs;
    var cuindex = e.target.dataset.index;
    imgs.splice(cuindex, 1);
    that.setData({
      imgs: imgs
    })
  },

  //预览图片
  viewimage: function (e) {
    var cuindex = e.target.dataset.index;
    var that = this;
    var urls = that.data.imgs;
    wx.previewImage({
      current: urls[cuindex], // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },

  submitInfo: function (e) {
    var that = this
    var formid=e.detail.formId;
    this.setData({
      formid: formid
    });
    wx.showModal({
      title: '提示',
      content: '一旦提交，信息无法修改，您确定提交吗',
      success (res) {
        if (res.confirm) {
          that.savestudent();
        } else if (res.cancel) {
          return
        }
      }
    })
  },
   //登记提交
  savestudent: function () {
    var schs = this.data.schs;
    var scindex = this.data.scindex;
    var csid = schs[scindex].CSID;
    if (csid==''){
      wx.showModal({
        title: '友情提示',
        content: '请选择你需要登记的学校!',
        showCancel: !1,
      })
      return;
    }

    var csnum = schs[scindex].CSNUM;
    var sname=this.data.sname;
    if (sname == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入儿童姓名!',
        showCancel: !1,
      })
      return;
    }

    var sfzh = this.data.sfzh;
    if (sfzh == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入儿童身份证号!',
        showCancel: !1,
      })
      return;
    }

    var birth = this.data.birth;
    var sex = this.data.sex;
    /** 
    if (birth == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入儿童生日，格式为20210203!',
        showCancel: !1,
      })
      return;
    }

    if (sex == '') {
      wx.showModal({
        title: '友情提示',
        content: '请选择性别!',
        showCancel: !1,
      })
      return;
    }
**/
    var jzxm = this.data.jzxm;
    if (jzxm == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入家长姓名!',
        showCancel: !1,
      })
      return;
    }

    var jzsj = this.data.jzsj;
    if (jzsj == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入家长手机!',
        showCancel: !1,
      })
      return;
    }

    var that = this;
    var imgs = this.data.imgs;
    //先上传图片
    if (imgs.length > 0) {
      wx.showLoading({
        title: '保存中',
      })
      that.setData({
        disabled: true,
        loading: true
      })
      //先上传图片
      that.uploadStuurl();
    } else {
      if(this.data.upurls.length <= 0){
        wx.showModal({
          title: '友情提示',
          content: '请上传户口本第一页或有详情地址页,登记儿童所在页,双胞胎的请分别提交两个孩子的户口所在页!',
          showCancel: !1,
        })
        return;
      }
      that.oksavestudent();
    }
  },
  


  uploadStuurl: function () {
    var that = this;
    var imgs = this.data.imgs;
    var upurls = this.data.upurls;
    const uploadTask = wx.uploadFile({
      url: app.myUlife.ulifeUrl + '/wechart/uploadBmurl.html',
      filePath: imgs[0],
      name: 'file',
      formData: {},
      success: function (res) {
        var data = JSON.parse(res.data);
        // 如果图片有违规操作
        if(!data.result){
          wx.showModal({
            title: '友情提示',
            content: data.msg,
            showCancel: !1,
          })
          wx.hideLoading();
          that.setData({
            disabled: false,
            loading: false
          })
          return;
        }
        var url = data.urls;
        upurls.push(url);
        that.setData({
          upurls: upurls
        });
        that.data.imgs.splice(0, 1);
        if (that.data.imgs.length > 0) {
          that.uploadStuurl();
        } else {
          that.oksavestudent();
        }
      }
    })
  },



  oksavestudent: function (e) {
    var that = this;
    var schs = this.data.schs;
    var scindex = this.data.scindex;
    var csid = schs[scindex].CSID;
    var csnum = schs[scindex].CSNUM;
    var sname = this.data.sname;
    var sfzh = this.data.sfzh;
    var jzxm = this.data.jzxm;
    var jzsj = this.data.jzsj;
    var upurls = this.data.upurls;
    var birth = this.data.birth;
    var sex = this.data.sex;

    //获取地区
    var region = this.data.region;
    var arindex = this.data.arindex;
    var town = region[arindex].TNID;
    var cun = region[arindex].VAID;
    var formid = this.data.formid;
    var djson = {
      csid: csid,
      csnum: csnum,
      sname: sname,
      sfzh: sfzh,
      birth: birth,
      sex: sex,
      jzxm: jzxm,
      jzsj: jzsj,
      town: town,
      cun: cun,
      urls: upurls,
      wxid: that.data.wxid,
      tsrc: "0",
      formid: formid
    };

    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/insertStudent.html',//请求地址
      data: djson,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          disabled: false,
          loading: false
        })

        if (res.data.result){
          wx.redirectTo({
            url: '../../pages/bmsuccess/bmsuccess'
          })
        }else{
          wx.showModal({
            title: '友情提示',
            content: res.data.msg,
            showCancel: !1,
          })
        }
       
      }
    })
  }

})
